import requests
from lxml import etree
import xlwt
import time


book =xlwt.Workbook(encoding='utf-8')
sheet=book.add_sheet('Sheet')
sheet.write(0,0,'python')
sheet.write(1,1,'love')
book.save('test.xls')

header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'}
urls=['https://www.qidian.com/all?page={}'.format(str(i)) for i in range(1,26995)]
all_info_list=[]

def get_info(url):
    html=requests.get(url,header)
    selector=etree.HTML(html.text)
    infos=selector.xpath('//ul[@class="all-img-list cf"]/li')
    #print(len(infos))
    for info in infos:
        title=info.xpath('div[2]/h4/a/text()')[0]
        link=info.xpath('div[2]/h4/a/@href')[0]
        author=info.xpath('div[2]/p[1]/a[1]/text()')[0]
        style_1=info.xpath('div[2]/p[1]/a[2]/text()')[0]
        style_2 = info.xpath('div[2]/p[1]/a[3]/text()')[0]
        style=style_1+'.'+style_2
        complete= info.xpath('div[2]/p[1]/span/text()')[0]
        introduce=info.xpath('div[2]/p[2]/text()')[0].strip()
        word=info.xpath('div[2]/p[3]/span/text()')[0].strip('万字')
        info_list = [title,link, author, style,complete,introduce,word]
        all_info_list.append(info_list)
for url in urls:
    get_info(url)
    head=['title','link','author','style','complete','introduce','word']
    book=xlwt.Workbook(encoding='utf-8')
    sheet=book.add_sheet('Sheet1')
    for h in range(len(head)):
        sheet.write(0,h,head[h])
    i=1
    for list in all_info_list:
        j=0
        for data in list:
            sheet.write(i,j,data)
            j+=1
        i+=1
    book.save('xiaoshuo.xls')








